package com.xcm.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * cookie操作工具
 *
 * @author 林强
 * @date 2018-04-12 11:15
 */
public class CookieUtils {

    public static boolean secure = false;

    private CookieUtils() {
    }

    /**
     * 按名称获取cookie
     *
     * @param request 请求
     * @param name    cookie名称
     * @return
     */
    public static String getCookieValueByName(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || StringUtils.isBlank(name)) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if (name.equals(cookie.getName())) {
                return cookie.getValue();
            }
        }
        return null;
    }

    /**
     * 按名称获取cookie
     *
     * @param request 请求
     * @param name    cookie名称
     * @return
     */
    public static Cookie getCookieByName(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || StringUtils.isBlank(name)) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if (name.equals(cookie.getName())) {
                return cookie;
            }
        }
        return null;
    }

    /**
     * 清除cookie
     *
     * @param response   响应
     * @param cookieName cookie名称
     */
    public static void removeCookie(HttpServletResponse response, HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || StringUtils.isBlank(cookieName)) {
            return;
        }
        for (Cookie cookie : cookies) {
            if (cookieName.equals(cookie.getName())) {
                Cookie cookieRemove = new Cookie(cookie.getName(), null);
                cookieRemove.setMaxAge(0);
                cookieRemove.setPath(cookie.getPath());
                response.addCookie(cookieRemove);
            }
        }
    }

    /**
     * 创建cookie
     *
     * @param request    请求
     * @param response   响应
     * @param cookieName cookie名称
     * @param value      cookie值
     */
    public static void createCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String value) {
        // Cookie添加token
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    /**
     * 创建cookie
     *
     * @param response   响应
     * @param cookieName cookie名称
     * @param value      cookie值
     * @param expiry     超时时间
     */
    public static void createCookie(HttpServletResponse response, String cookieName, String value, int expiry, boolean secure) {
        // Cookie添加token
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setPath("/");
        cookie.setSecure(secure);
        cookie.setMaxAge(expiry);
        response.addCookie(cookie);
    }
}
